/**
 * 应用配置管理
 * 统一管理所有环境变量和配置项
 */

// 应用基础配置
export const appConfig = {
  title: import.meta.env.VITE_APP_TITLE || 'AI文案生成器',
  version: import.meta.env.VITE_APP_VERSION || '1.0.0',
  environment: import.meta.env.VITE_APP_ENV || 'development',
} as const;

// API配置
export const apiConfig = {
  baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000/api',
  timeout: parseInt(import.meta.env.VITE_API_TIMEOUT || '10000'),
} as const;

// 开发服务器配置
export const devConfig = {
  port: parseInt(import.meta.env.VITE_DEV_SERVER_PORT || '5173'),
  host: import.meta.env.VITE_DEV_SERVER_HOST || 'localhost',
} as const;

// 功能开关配置
export const featureConfig = {
  analytics: import.meta.env.VITE_ENABLE_ANALYTICS === 'true',
  debug: import.meta.env.VITE_ENABLE_DEBUG === 'true',
  hotReload: import.meta.env.VITE_ENABLE_HOT_RELOAD === 'true',
  sourceMap: import.meta.env.VITE_ENABLE_SOURCE_MAP === 'true',
  mockData: import.meta.env.VITE_ENABLE_MOCK_DATA === 'true',
  vueDevTools: import.meta.env.VITE_ENABLE_VUE_DEVTOOLS === 'true',
  eslint: import.meta.env.VITE_ENABLE_ESLINT === 'true',
  compression: import.meta.env.VITE_ENABLE_COMPRESSION === 'true',
  cache: import.meta.env.VITE_ENABLE_CACHE === 'true',
} as const;

// 存储配置
export const storageConfig = {
  prefix: import.meta.env.VITE_STORAGE_PREFIX || 'ai_copywriting_',
} as const;

// 日志配置
export const logConfig = {
  level: import.meta.env.VITE_LOG_LEVEL || 'info',
  console: import.meta.env.VITE_ENABLE_CONSOLE_LOG === 'true',
} as const;

// 模拟数据配置
export const mockConfig = {
  enabled: import.meta.env.VITE_ENABLE_MOCK_DATA === 'true',
  delay: parseInt(import.meta.env.VITE_MOCK_DELAY || '1000'),
} as const;

// 环境判断工具
export const isDevelopment = appConfig.environment === 'development';
export const isProduction = appConfig.environment === 'production';
export const isTest = appConfig.environment === 'test';

// 配置验证
export function validateConfig() {
  const requiredConfigs = [
    { key: 'VITE_APP_TITLE', value: appConfig.title },
    { key: 'VITE_API_BASE_URL', value: apiConfig.baseURL },
  ];

  const missingConfigs = requiredConfigs.filter(config => !config.value);
  
  if (missingConfigs.length > 0) {
    console.warn('⚠️ 缺少必需的环境变量:', missingConfigs.map(c => c.key));
  }

  if (featureConfig.debug) {
    console.log('🔧 应用配置:', {
      app: appConfig,
      api: apiConfig,
      features: featureConfig,
      storage: storageConfig,
      log: logConfig,
      mock: mockConfig,
    });
  }
}

// 导出所有配置
export const config = {
  app: appConfig,
  api: apiConfig,
  dev: devConfig,
  features: featureConfig,
  storage: storageConfig,
  log: logConfig,
  mock: mockConfig,
  isDevelopment,
  isProduction,
  isTest,
} as const;

export default config; 